<?php
/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2018/3/21
 * Time: 15:35
 */
namespace app\admin\controller;
use app\common\controller\Curt;
use Library\Utils;
use think\Db;
use think\Config;
use think\Request;
class  Goods extends Curt
{
    function __construct()
    {
        parent::__construct($upload = true,$uploadConfig = array(
            'input_name' => 'coverFile',//文本框name
            'data_field' => 'goods_image'//数据库字段
        ));
    }
    /*
     * 商品列表
     * $keyword String  【关键字搜索-goods_name】
     */
    public function showList()
    {
        $keyword = input('get.keyword');
        if ($keyword) {
            $where['a.goods_name|b.classify_name|c.shop_name'] = ['like', '%' . $keyword . '%'];  //模糊查询or可以一个条件中写多个字段以‘|’分开
        }
        $list = Db::name('goods')
            ->alias('a')
            ->join('ec_goodsclassify b','a.classify_id = b.id',left)
            ->join('ec_shop c','a.shop_id = c.id',left)
            ->order('a.id desc')
            ->where($where)
            ->field('a.*,b.classify_name,c.shop_name')
            ->fetchSql(false)
            ->paginate();
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * @param $id
     * @return mixed
     * 商品编辑
     */
    public function showEdit($id)
    {
        $select = $this->model->find($id);
        if (empty($select)) {
            $this->error('非法操作');
        }
        $select=$select->toArray();
        $img=explode(',',$select['good_urlimg']);
        $onfig=[];
        foreach ($img as $k=>$v){
            $onfig[$k]['caption']=$v;
            $onfig[$k]['width']="120px";
            $onfig[$k]['url']="deletes";
            $onfig[$k]['key']=$v;
        }
        $list=Db('goodsclassify')->where('status',1)->select();
        $Utils= new Utils();
        $list= $Utils::togoodsclassify($list,'father_id');
        $this->assign('classlist',$list);
        $this->assign('data', $select);
        $this->assign('config', json_encode($onfig));
        $this->assign('path',json_encode($img));
        return $this->fetch();
    }


    /*
     * 商品商情
     */
    public function details($id)
    {
        $info = Db::name('goods')
            ->alias('a')
            ->join('ec_goodsclassify b','a.classify_id = b.id',left)
            ->join('ec_shop c','a.shop_id = c.id',left)
            ->field('a.*,b.classify_name,c.shop_name')
            ->find($id);
        $info['good_urlimg']=explode(',',$info['good_urlimg']);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /*
     * 添加数据
     */
    public function goodsadd()
    {
        if (request()->isPost()) {
            $data = $this->request->param();
            $validate = validate($this->table);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $file = request()->file($this->uploadsconfig['input_name']);
            if ($file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size' => $this->uploads['SIZE'], 'ext' => $this->uploads['EXT']])->move($this->uploads['PATH']);
                if ($info) {
                    $data['goods_image'] = date("Ymd") . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data = $this->createData($data);
            $res = $this->model->allowField(true)->save($data);
            if ($res) {
                $this->success('添加成功', $this->fetchconfig['add_success']);
            } else {
                $this->error('添加失败', $this->fetchconfig['add_success']);
            }
        } else {
            $list=Db('goodsclassify')->where('status',1)->select();
            $Utils= new Utils();
            $list= $Utils::togoodsclassify($list,'father_id');
            $this->assign('classlist',$list);
            return $this->fetch();
        }

    }

    /*
    * 商品上下架操作
    */
    public function operation()
    {
        $status = Request::instance()->param('goods_status');
        $where['id'] = Request::instance()->param('id');
        $station = $status == 1 ? 2 : 1;
        if ($where['id']) {
            $res = $this->model->where($where)->setField('goods_status', $station);
            if ($res) {
                $this->success('操作成功', $this->fetchconfig['add_success']);
            } else {
                $this->error('网络繁忙，请稍后重试', $this->fetchconfig['add_success']);
            }
        }
    }
   /*
    * 删除数据
    */
    public function delgood($id)
    {
        $this->model->where(['id' => $id])->delete();
        $this->success('删除' . 1 . '条数据', $this->fetchconfig['add_success']);
    }


    /*
  * 商品是否在首页显示操作
  */
    public function isshow()
    {
        $status = Request::instance()->param('is_show');//取值同input
        $data['id'] = Request::instance()->param('id');
        $data['is_show'] = $status == 1 ? 2 : 1;
        if ($data['id']) {
            $res = $this->model->allowField(true)->isUpdate(true)->save($data);
            if ($res) {
                $this->success('操作成功', $this->fetchconfig['edit_success']);
            } else {
                $this->error('网络繁忙，请稍后重试', $this->fetchconfig['edit_success']);
            }
        }
    }

}